#pragma once

#include "CoreMinimal.h"
#include "WiiRemoteDevice.h"
#include "Runtime/InputDevice/Public/IInputDeviceModule.h"
#include <Modules/ModuleManager.h>
#include <Modules/ModuleInterface.h>

class FWiiRemoteDeviceModule : public IInputDeviceModule
{
public:
    virtual TSharedPtr<class IInputDevice> CreateInputDevice(const TSharedRef< FGenericApplicationMessageHandler >& InMessageHandler) override;

    virtual void ShutdownModule();

    FString GetModulePriorityKeyName() const
    {
        return FString(TEXT("WiiRemoteDevice"));
    }

    FORCEINLINE TSharedPtr<class FWiiRemoteDevice> GetWiimoteDevice() const
    {
        return WiiDevice;
    }

    static inline class FWiiRemoteDevice* GetWiimoteDeviceSafe()
    {
#if WITH_EDITOR
        FWiiRemoteDevice* Device = nullptr;

        if (!FWiiRemoteDeviceModule::IsAvailable())
        {
            UE_LOG(LogClass, Log, TEXT("FWiiRemoteDeviceModule Is not Available!"));
            return Device;
        }

        Device = FWiiRemoteDeviceModule::IsAvailable() ? FWiiRemoteDeviceModule::Get().GetWiimoteDevice().Get() : nullptr;
#else
        FWiimoteInputDevice* Device = FWiiRemoteDeviceModule::Get().GetWiimoteDevice().Get();
#endif
        return Device;
    }

    static inline FWiiRemoteDeviceModule& Get()
    {
        return FModuleManager::LoadModuleChecked<FWiiRemoteDeviceModule>("WiiRemoteDevice");
    }

    static inline bool IsAvailable()
    {
        return FModuleManager::Get().IsModuleLoaded("WiiRemoteDevice");
    }

private:
    TSharedPtr<FWiiRemoteDevice> WiiDevice;
};